//package com.tanchengjin.admin.modules.cms.controller;
//
//import com.github.pagehelper.PageHelper;
//import com.tanchengjin.admin.modules.cms.mapper.CategoryMapper;
//import com.tanchengjin.admin.modules.cms.pojo.ArticleCategory;
//import com.tanchengjin.admin.model.vo.CategoryVO;
//import com.tanchengjin.admin.service.ArticleCategoryService;
//import com.tanchengjin.admin.utils.LayuiAdminServerResponse;
//import com.tanchengjin.admin.utils.ServerResponse;
//import com.tanchengjin.admin.validations.CategoryValidate;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * 文章分类
// * @author tanchengjin
// */
//@Controller
//@RequestMapping("${app.admin.prefix}/categoryManager")
//public class CategoryManagerController {
//    @Autowired
//    private CategoryMapper categoryMapper;
//
//    @Autowired
//    private ArticleCategoryService articleCategoryService;
//
//    @RequestMapping(value = "/list", method = {RequestMethod.GET})
//    @ResponseBody
//    public LayuiAdminServerResponse index(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit) {
//        PageHelper.startPage(page, limit);
//        List<ArticleCategory> articleCategoryList = articleCategoryService.getAll();
//        return LayuiAdminServerResponse.responseWithSuccess(articleCategoryList, String.valueOf(categoryMapper.count()));
//    }
//
//    @RequestMapping(value = "/index", method = {RequestMethod.GET})
//    public String indexView() {
//        return "/admin/category/index";
//    }
//
//    @RequestMapping(value = "/create", method = {RequestMethod.GET})
//    public String create(Model model) {
//        List<ArticleCategory> articleCategoryList = articleCategoryService.getAll();
//        model.addAttribute("articleCategoryList", articleCategoryList);
//        return "/admin/category/add";
//    }
//
//    @RequestMapping(value = "", method = {RequestMethod.POST})
//    @ResponseBody
//    public ServerResponse store(@Validated CategoryValidate categoryValidate) {
//        ArticleCategory ArticleCategory = new ArticleCategory();
//        BeanUtils.copyProperties(categoryValidate, ArticleCategory);
//        int i = articleCategoryService.create(ArticleCategory);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailureMessage("创建失败请重试");
//        }
//    }
//
//    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
//    @ResponseBody
//    public ServerResponse<ArticleCategory> show(@PathVariable int id) {
//        ArticleCategory ArticleCategory = categoryMapper.selectByPrimaryKey(id);
//        return ServerResponse.responseWithSuccess(ArticleCategory);
//    }
//
//    @RequestMapping(value = "/{id}/edit", method = {RequestMethod.GET})
//    public String edit(Model view, @PathVariable(value = "id") int id) {
//        ArticleCategory articleCategory = categoryMapper.selectByPrimaryKey(id);
//        view.addAttribute("Category", articleCategory);
//        return "/admin/category/edit";
//    }
//
//    @RequestMapping(value = "/{id}", method = {RequestMethod.PUT})
//    @ResponseBody
//    public ServerResponse update(@Validated CategoryVO categoryVO, @PathVariable("id") int id) {
//        ArticleCategory articleCategory = new ArticleCategory();
//        BeanUtils.copyProperties(categoryVO, articleCategory);
//        articleCategory.setId(id);
//        int i = categoryMapper.updateByPrimaryKeySelective(articleCategory);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailure(0, "更新失败，请重试", null);
//        }
//    }
//
//    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
//    @ResponseBody
//    public ServerResponse destroy(@PathVariable(value = "id") int id) {
//        int i = categoryMapper.deleteByPrimaryKey(id);
//        if (i >= 1) {
//            return ServerResponse.responseWithSuccess();
//        } else {
//            return ServerResponse.responseWithFailure(0, "删除失败请重试", null);
//        }
//    }
//
//    private List<CategoryVO> articleConvertArticleVO(List<ArticleCategory> d) {
//        ArrayList<CategoryVO> articleVOS = new ArrayList<>();
//        d.stream().forEach(e -> {
//            CategoryVO categoryVO = this.articleConvertArticleVO(e);
//            articleVOS.add(categoryVO);
//        });
//
//        return articleVOS;
//    }
//
//    private CategoryVO articleConvertArticleVO(ArticleCategory d) {
//        CategoryVO categoryVO = new CategoryVO();
//        BeanUtils.copyProperties(d, categoryVO);
//        return categoryVO;
//    }
//}
